package com.zt.springcloud.controller;

import cloud.zt.springboot.utils.ResultUtils;
import cloud.zt.springboot.utils.StringUtils;
import cloud.zt.springboot.utils.TokenUtils;
import com.zt.springcloud.entity.Cart;
import com.zt.springcloud.entity.Product;
import com.zt.springcloud.entity.Vo.CartVo;
import com.zt.springcloud.service.FoodService;
import com.zt.springcloud.service.ShopCartService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author RedReins
 * @version 1.0
 * @description: 购物车
 * @date 2021/9/19 10:54
 */

@RestController
@RequestMapping("/shopcart")
public class ShopCartController {
    private static Logger logger = LoggerFactory.getLogger(ShopCartController.class);
    @Autowired
    private ShopCartService shopCartService;
    @Autowired
    private FoodService foodService;
    /*
     * 添加购物车
     * */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResultUtils<?> addShopCart(Cart cart){
        cart.setUsername(TokenUtils.getUsername());
        Boolean b = shopCartService.insertCart(cart);
        if(b){
            return ResultUtils.success("添加购物车成功!");
        }
        return ResultUtils.fail("添加购物车失败!");
    }

    /*查詢所有购物车商品*/
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ResultUtils<List<CartVo>> getCartList(){
        List<CartVo> cartList = shopCartService.getCartList();
        return ResultUtils.success(cartList);
    }
    /*
     * 增加购物车中商品的购买数量
     * */

    @RequestMapping(value = "/addAmount/{itemid}",method = RequestMethod.PUT)
    public ResultUtils<?> addShopCartAmount(@PathVariable("itemid") Long itemid){
        Product itemInfo = foodService.getItemInfo(itemid);
        if(!StringUtils.isNull(itemInfo)){
            return ResultUtils.success(shopCartService.addShopCartAmount(itemid));
        }
        return ResultUtils.fail();
    }
    /*
     * 减少加购物车中商品的购买数量
     * */

    @RequestMapping(value = "/minusAmount/{itemid}",method = RequestMethod.PUT)
    public ResultUtils<?> minusShopCartAmount(@PathVariable("itemid") Long itemid){
        Product itemInfo = foodService.getItemInfo(itemid);
        if(!StringUtils.isNull(itemInfo)){
            return ResultUtils.success(shopCartService.minusShopCartAmount(itemid));
        }
        return ResultUtils.fail();
    }

    /*
    * 根据商品ID查询购物车中的商品
    * */
    @RequestMapping(value = "/listByIds",method = RequestMethod.POST)
    public ResultUtils<?> findItemsByIds(@RequestBody String itemIds){
        List<CartVo> products = shopCartService.findItemsByIds(itemIds);
        return ResultUtils.success(products);

    }





}
